"""
使用python编写小7项目的加密方法--异或处理--对应蒋文接口的 SignType= 1
"""
# coding=utf-8
import base64
import hashlib
import random
from time import time

import requests

from config import encrypted_string


def get_timestamp():
    timestamp = int(round(time()))
    return timestamp


def get_random_num():
    random_num = "Test" + str(random.randint(10, 10000))
    return random_num


def build_query(query_arr):
    query_arr_list = []
    for key in query_arr:
        query_arr_list.append(key + "=" + str(query_arr[key]))
    query_arr_list.sort()
    arr = "&".join(query_arr_list)
    return arr


def get_encrypt_string(domain, os_type):
    encrypt_list = {
        "market": {
            1: encrypted_string.market_IOS_EncryString,
            2: encrypted_string.market_Android_EncryString
        },
        "pay": {
            1: encrypted_string.pay_IOS_EncryString,
            2: encrypted_string.pay_Android_EncryString
        },
        "user": {
            1: encrypted_string.user_IOS_EncryString,
            2: encrypted_string.user_Android_EncryString
        },
        "buy": {
            1: encrypted_string.buy_IOS_EncryString,
            2: encrypted_string.buy_Android_EncryString
        },
        "developer": {
            1: encrypted_string.developer_IOS_EncryString,
            2: encrypted_string.developer_Android_EncryString
        },
        "h5game": {
            1: encrypted_string.hgame_Android_EncryString,
            2: encrypted_string.hgame_IOS_EncryString
        },
        "japi": {
            1: encrypted_string.japi_IOS_EncryString,
            2: encrypted_string.japi_Android_EncryString
        }

    }
    string = encrypt_list.get(domain).get(os_type)
    return string


def build_xor_encrypt(query_arr, domain, os_type):
    xor_encrypt = str()
    arr = build_query(query_arr)
    string = get_encrypt_string(domain, os_type)
    for k in range(len(arr)):
        a = chr(ord(arr[k]) ^ ord(string[k % len(string)]))
        xor_encrypt += a
    auth_string = base64.encodebytes(xor_encrypt.encode('utf-8'))
    auth_string = bytes(auth_string).decode('utf-8').replace("\n", "").replace("/", "_").replace("+","-").rstrip("=")
    return auth_string


def encrypt_xor_param(query_arr, domain, os_type):
    param = {"timeStamp": get_timestamp(),
             "randomNum": get_random_num()}
    query_arr.update(param)
    auth_string = {"auth_string": build_xor_encrypt(query_arr, domain, os_type)}
    param.update(auth_string)
    encrypt_string = get_encrypt_string(domain, os_type)
    sign = build_query(param) + encrypt_string
    sign = hashlib.md5(sign.encode('UTF-8')).hexdigest()
    sign = {"sign": sign}
    query_arr.update(auth_string)
    query_arr.update(sign)
    param = build_query(query_arr)
    return param








# if __name__=='__main__':
#     def simple():
#         url = "https://market.msshuo.cn/ImUserGroup/applyJoinMemberGroup"
#         headers_data = {
#             "Content-Type": "application/x-www-form-urlencoded",
#             "User-Agent": "X7Market/4.81.999.666/Android/10/phone/HUAWEI_AQM-AL10/1983bd511f7cc928a371e04dd442db7f/bed146e99d83b5c53abbce884ba9f1f7",
#             "x7sy-dev": "oaid=12b09973-6d7f-4023-a462-34ed025bfaaa&imei=-1&new_uuid=bed146e99d83b5c53abbce884ba9f1f7&uuid=1983bd511f7cc928a371e04dd442db7f&is_simulator=-1&idfa=&tgid=1&sign=2f6a19a7c066ecddf0cd4ce0f811332f&ua=Mozilla/5.0 (Linux; Android 10; AQM-AL10 Build/HONORAQM-AL10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36",
#             "i18n": "2/2/-1/GMT+08:00/-1",
#             "JWT-Encoding": "",
#             "X7DeviceToken": "TkVXSUQjYjA3MTc4ZDVmNTc5YWU2YTBlNDAwYTRhMTQ1MTk5NjMtaC0xNjE5NTI2MTQwMTIxLTI5NTgyOTdjMzIzMTQzNTdiMzkxYWI4Yzc0ZWMzMjA2I1pMLzdjNFRGclZVNUYxWU9XY2hkc0RHVkFYd1hDZXNUdjBzdUxNK0MzQXVldEtvdVpHSGpSZkxwSGJwR0Q1T0hyNXh0Q0RQMUo2OTJvdGVLRDBOSXlDSC9pajdKakxuQmF4WHJ4NS90Zm0rclJpL3JvS2V1dTFGQkdZby91RDhZNXJ2a3d4TEwwN2FQMVhZbkgzZVNZWkgwN3ZmUzR5YmxPbGZSUjRqZk12MnBWTzEzTlIrNE5SV00xZVlLS0h6Y1VEVDVDNTkrOEhCakdReXQxTTNLZ2lVMTNOeEtEWnM2S21EYUNWR2tsUT09"
#         }
#         request_data = {
#             "mid": "1005",
#             "applyReply":"777",
#             "groupId": "1314"
#         }
#         data1 = encrypt_xor_param(request_data, "market", 2)
#         print(data1)
#         resp = requests.post(url=url,headers=headers_data,data=data1)
#         return resp.json()
#
# print(simple())